/**
 * $Id: SubmitDeliverRespThread.java,v 1.12 2006/07/17 06:00:32 star Exp $
 *
 * Copyright 2006 Skyinfo, Inc. All Rights Reserved.
 */
package com.haoxi.gateway.runner;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.haoxi.gateway.common.CommonMsgBean;
import com.haoxi.gateway.common.Queue;
import com.haoxi.gateway.connector.Connector;
import com.haoxi.gateway.runner.smscommand.CommandMOResp;
import com.haoxi.gateway.runner.smscommand.CommandReportResp;

/**
 * TODO
 * 
 * Revision History
 * 
 * 2006-6-12,star.Zhao,created it
 */
public class SubmitDeliverRespThread extends AbsOutQueue {
    private static final Log log = LogFactory
            .getLog(SubmitDeliverRespThread.class);
    private Map canSubmitDeliverRespConn = new HashMap();

    public SubmitDeliverRespThread(Queue queue) {
        super(queue);
    }

    public int doJob(Object pObject, int queueLen) {
        int nRet = AbsOutQueue.PROC_ERROR;
        if (pObject instanceof CommonMsgBean) {
            CommonMsgBean msgObj = (CommonMsgBean) pObject;
            // 若下发的是MT，记录MT到Map中
            Object cmd = msgObj.getReserve();
            String connName = msgObj.getConnName();
            if (cmd instanceof CommandMOResp
                    || cmd instanceof CommandReportResp) {
                // dynamic get canSubmitDeliverRespConn
                canSubmitDeliverRespConn = RunnerMgr.getInstance()
                        .getGatewayInfo().getCanReciveDeliverConns();

                if (canSubmitDeliverRespConn.containsKey(connName)) {
                    Connector conn = (Connector) canSubmitDeliverRespConn
                            .get(connName);

                    if (conn.isClose()) {
                        nRet = AbsOutQueue.PROC_ERROR;
                        log.error(connName
                                + " is close. DeliverResp is thrown! "
                                + cmd.toString());
                    } else {
                        try {
                            if (!conn.writeProtcol(msgObj)) {
                                nRet = AbsOutQueue.PROC_ERROR;
                                log.error(connName
                                        + " send Resp unsucessfully! "
                                        + cmd.toString());
                            } else {
                                nRet = AbsOutQueue.PROC_NORMAL;
                                log.debug(" send Resp sucessfully! " + cmd.toString());
                            }
                        } catch (IOException e) {
                            nRet = AbsOutQueue.PROC_ERROR;
                            log.error(connName
                                    + "send deliver Resp msg IOException:", e);
                        }
                    }

                } else {
                    // =unkown object message !!!!!!!!!!!!!!!!!!!!!
                    log.error("have unkown where recived the message of Resp!");
                    nRet = AbsOutQueue.PROC_ERROR;
                }

            } else {
                // unkown object message !!!!!!!!!!!!!!!!!!!!!
                log.error("have unkown object message !");
                nRet = AbsOutQueue.PROC_ERROR;
            }
        }
        return nRet;
    }
}
